package com.atguigu.dga.meta.service.impl;

import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.constants.MetaConst;
import com.atguigu.dga.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-26
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {


    public void initTableMetaInfoExtra(List<TableMetaInfo> tableMetaInfoList) {

        List<TableMetaInfoExtra> tableMetaInfoExtraList = new ArrayList<>(tableMetaInfoList.size());

        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            TableMetaInfoExtra tableMetaInfoExtra = new TableMetaInfoExtra();
            tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
            tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());

            tableMetaInfoExtra.setBusiOwnerUserName(MetaConst.OWNER_UNSET);
            tableMetaInfoExtra.setTecOwnerUserName(MetaConst.OWNER_UNSET);

            tableMetaInfoExtra.setLifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET);
            tableMetaInfoExtra.setLifecycleDays(-1L);

            tableMetaInfoExtra.setSecurityLevel(MetaConst.SECURITY_LEVEL_UNSET);
            tableMetaInfoExtra.setDwLevel(getDwLevel(tableMetaInfo.getTableName()));
            tableMetaInfoExtra.setCreateTime(new Date());
            tableMetaInfoExtraList.add(tableMetaInfoExtra);

        }

        saveBatch(tableMetaInfoExtraList);

    }
        private String getDwLevel(String tableName){
            //根据表名的前缀，获得分层名称 ods ,dwd,dws,ads,dm,dim  返回MetaConst中分层常量
            if(tableName.startsWith("ods")){
                return "ODS";
            } else if (tableName.startsWith("dwd")) {
                return "DWD";
            }else if (tableName.startsWith("dim")) {
                return "DIM";
            }else if (tableName.startsWith("dws")) {
                return "DWS";
            }else if (tableName.startsWith("ads")) {
                return "ADS";
            }else if (tableName.startsWith("dm")) {
                return "DM";
            }else  {
                return "OTHER";
            }



        }


}
